﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using androidpost.Utils;


namespace androidpost
{
    public partial class login : Form
    {
        String so = "nouser";
        
        WebClient cGet = new WebClient();
        public login()
        {
            InitializeComponent();
        }

        private void login_Load(object sender, EventArgs e)
        {

           
            startcheck fomcheck = new startcheck();
            fomcheck.ShowDialog();
            modRegistry modRegistry = new modRegistry();
            if (modRegistry.Read("check")=="1")
            {
                txtUsername.Text=modRegistry.Read("user");
                txtpass.Text = modRegistry.Read("pass");
                checkBox1.Checked = true;
            }

        }
        private void startConnect()
        {
            
                try
                {
                   // cGet.Encoding = System.Text.Encoding.UTF8;
                   
                    so = cGet.DownloadString(Constant.domain+"checklogin.php?id=" + txtUsername.Text + txtpass.Text);
                   // System.Web.HttpUtility.UrlEncode( str ).Replace(”+”,”%20″);
                   
                    if (so.IndexOf("nouser") == -1) Constant.id = txtUsername.Text + txtpass.Text;
                    else MessageBox.Show("Wrong username or password");
                }
                catch (Exception)
                {
                    so = "nouser";
                    MessageBox.Show("Connection is lost");
                    
                    
                }


        }

        private void button1_Click(object sender, EventArgs e)
        {

 
            modRegistry abc123 = new modRegistry();
            if (checkBox1.Checked == true)
            {

                abc123.Write("user", txtUsername.Text);
                abc123.Write("pass", txtpass.Text);
                abc123.Write("check", "1");
            }
            else abc123.Write("check", "0"); ;

            button1.Text = "Signing in";
            button1.Enabled = false;
           // startConnect();

            string md5Pass = Utils1.md5(txtpass.Text);
            Object.OJStatus re1 = Network.NetworkSupport.checkLogin(txtUsername.Text, md5Pass);


          if (re1.code == 0)
          {

              Constant.id = txtUsername.Text + md5Pass;
              Constant.GCM_REGID = re1.data;
              ControlMusic fom1 = new ControlMusic();
              fom1.Show();
              this.Hide();
          }
          else
          {

              button1.Text = "Login";
              Utils1.log(re1.result);
              MessageBox.Show(re1.result);
              button1.Enabled = true;
          }

            
        }

        private void login_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (1 == 1) Application.ExitThread();
           
            
        }

        private void login_FormClosed(object sender, FormClosedEventArgs e)
        {
            
        }

        private void label4_Click(object sender, EventArgs e)
        {
            register fomdk = new register();
            fomdk.Show();
            this.Hide();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.ExitThread();
        }

        private void txtEmail_TextChanged(object sender, EventArgs e)
        {

        }

       
    }
}
